import { FileResult } from "tmp";
import { SRMQTTDeviceAlert, SRMqttFile, SRMQTTString } from "../../starriver-task/model/sr.model";
import { SRDeviceAlertStatusEnum, SRFileTypeEnum } from "../../starriver-task/model/sr.enum";
import { V3proAlertMessageErrTypeEnum, V3proSubjectMessageTypeEnum } from "./v3pro.enum";
import {
  V3PanelBadPixel,
  V3proSendBox,
  V3proSendBoxScanBoard,
  V3proSendBoxScanBoardPanel,
} from "../../../api/v3pro-api/v3pro.api.response.model";

/**
 * starriver mqtt description 中的 file
 */
export class V3proAlertMessageFile extends SRMqttFile {
  constructor(type: SRFileTypeEnum, file: FileResult, hash: string, fileName: string) {
    super(type);
    this.file = file;
    this.hash = hash;
    this.fileName = fileName;
  }

  file: FileResult;
  hash: string;
  fileName: string;
}

/**
 * v3pro 错误信息
 */
export class V3proSubjectMessage extends SRMQTTDeviceAlert implements SRMQTTString {
  alertType?: V3proAlertMessageErrTypeEnum;
  msgType?: V3proSubjectMessageTypeEnum;
  srMqttTopic?: string;
  files?: V3proAlertMessageFile[];
  error?: string;
  needUploadFile?: boolean;
  screenId?: string;
  keyParams?: any;
  param?: string;

  /**
   * 将消息设置为异常
   * 默认消息为正常
   */
  setErrStatus() {
    this.status = SRDeviceAlertStatusEnum.DAS_ABNORMAL;
  }

  /**
   * 判断消息是否为异常
   */
  isErrStatus() {
    return this.status === SRDeviceAlertStatusEnum.DAS_ABNORMAL;
  }
}

/**
 * 发送盒ip
 */
export class SendboxKey {
  constructor(
    public id: string,
    public ip: string,
  ) {}
  static build({ deviceInfo: { id }, addr: ip }: V3proSendBox) {
    return new SendboxKey(id, ip);
  }
}

/**
 * 扫描板ip
 */
export class ScanBoardKey {
  constructor(
    public id: string,
    public ip: string,
    xywh: [number, number, number, number],
  ) {}
  static build({
    deviceInfo: { id },
    addr: ip,
    layout: { x, y, width: w, height: h },
  }: V3proSendBoxScanBoard) {
    return new ScanBoardKey(id, ip, [x, y, w, h]);
  }
}

interface SRPanel {
  index: number;
  badPixels?: V3PanelBadPixel;
}

/**
 * 扫描板面板索引
 */
export class PanelGroupKey {
  public "panel-height": number;
  public "panel-width": number;
  public panels: SRPanel[];
  constructor(
    public id: string,
    public ip: string,
    public xywh: [number, number, number, number],
    panelWidth: number,
    panelHeight: number,
    panels: V3proSendBoxScanBoardPanel[],
  ) {
    this["panel-height"] = panelHeight;
    this["panel-width"] = panelWidth;
    this.panels = panels.map((panel, index) => ({
      index: panel.index,
      scanBoardId: id,
      badPixels: panel?.panelBadPixels,
    }));
  }

  static build(scanBoard: V3proSendBoxScanBoard, panels: V3proSendBoxScanBoardPanel[]) {
    return new PanelGroupKey(
      scanBoard.deviceInfo.id,
      scanBoard.addr,
      [scanBoard.layout.x, scanBoard.layout.y, scanBoard.layout.width, scanBoard.layout.height],
      scanBoard.status.panelConf?.width || 0,
      scanBoard.status.panelConf?.height || 0,
      panels,
    );
  }
}
